package com.springapp.jsonmodel;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.springapp.model.ItemInOrder;
import com.springapp.model.Order;
import com.springapp.model.OrderStatus;

/*
 * 
 * @uthor Sim4ik
 * 
 */
public class OrderJson {

    private Integer id;
    private Integer deskId;
    private OrderStatus status;
    private List<ItemInOrderJson> items;

    public OrderJson() {
    }
    
    public OrderJson(Order order){
    	this.id = order.getId();
    	this.deskId = order.getDesk().getId();
    	this.status = order.getStatus();
    	
    	List<ItemInOrder> itemInOrder = order.getItemsInOrder();
        items = new ArrayList<ItemInOrderJson>();
        for (int i = 0; i < itemInOrder.size(); i++) {
            items.add(ItemInOrderJson.toModel(itemInOrder.get(i)));
        }
    }

    public static OrderJson toModel(Order order) {
        OrderJson oj = new OrderJson(order);
        return oj;
    }
    
    public static Order fromModel(OrderJson json){
    	Order order = new Order();
        order.setStatus(json.getStatus());
        List<ItemInOrderJson> itemsInOrderJson = json.getItems();
        List<ItemInOrder> itemsInOrder = new ArrayList<>();
        
         for (ItemInOrderJson anItemsInOrderJson : itemsInOrderJson)
         {
             ItemInOrder itemInOrder = new ItemInOrder();
             itemInOrder.setQuantity(anItemsInOrderJson.getCount());
             itemsInOrder.add(itemInOrder);
         }
         
         return order;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getDeskId() {
        return deskId;
    }

    public void setDeskId(Integer deskId) {
        this.deskId = deskId;
    }

    public List<ItemInOrderJson> getItems() {
        return items;
    }

    public void setItems(List<ItemInOrderJson> items) {
        this.items = items;
    }

    public OrderStatus getStatus() {
        return status;
    }

    public void setStatus(OrderStatus status) {
        this.status = status;
    }
}